package com.momo.mal.manga.adapter;

import java.util.ArrayList;
import java.util.List;

import net.momo.mal.R;
import com.momo.mal.entity.Anime;
import com.momo.mal.entity.Manga;
import com.momo.mal.util.DrawableManager;

import android.content.Context;
import android.graphics.Color;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MangaResultAdapter extends BaseAdapter {
	private List<Manga> mangas = new ArrayList<Manga>();
	private LayoutInflater mInflater;
	private DrawableManager drawableManager;
	
	public MangaResultAdapter(Context context, List<Manga> mangas) {
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.mangas = mangas;
		drawableManager = new DrawableManager(context);
	}

	@Override
	public int getCount() {
		return mangas.size();
	}

	@Override
	public Object getItem(int location) {
		return mangas.get(location);
	}

	@Override
	public long getItemId(int location) {
		return location;
	}

	@Override
	public View getView(int location, View view, ViewGroup viewgroup) {
		ViewHolder holder;
		if(view == null) {
			holder = new ViewHolder();
			
			view = mInflater.inflate(
					R.layout.manga_result_adapter, null);
			
			holder.title = (TextView) view.findViewById(R.id.text_title);
			holder.synopsis = (TextView) view.findViewById(R.id.text_synopsis);
			holder.type = (TextView) view.findViewById(R.id.text_type);
			holder.volumes = (TextView) view.findViewById(R.id.text_volumes);
			holder.chapters = (TextView) view.findViewById(R.id.text_chapters);
			holder.score = (TextView) view.findViewById(R.id.text_score);
			holder.image = (ImageView) view.findViewById(R.id.image_manga);
			
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}
		
//		if(location % 2 == 0) {
//			view.setBackgroundColor(Color.rgb(180, 205, 250));
//		} else {
//			view.setBackgroundColor(Color.TRANSPARENT);
//		}
		
		holder.title.setText(mangas.get(location).getTitle());
		if(mangas.get(location).getSynopsis().length() > 94) {
			holder.synopsis.setText(Html.fromHtml(mangas.get(location).getSynopsis().substring(0, 94) + " ..."));
		} else {
			holder.synopsis.setText(Html.fromHtml(mangas.get(location).getSynopsis()));
		}
		holder.type.setText("Type : " + mangas.get(location).getType());
		holder.volumes.setText("Chapters : " + mangas.get(location).getChapters());
		holder.chapters.setText("Volumes : " + mangas.get(location).getVolumes());
		holder.score.setText("Score : " + mangas.get(location).getMemberScore());
		
		drawableManager.fetchDrawableOnThread(mangas.get(location).getImageUrl(), holder.image);
		
		return view;
	}
	
	private class ViewHolder {
		public TextView title;
		public TextView synopsis;
		public TextView type;
		public TextView volumes;
		public TextView chapters;
		public TextView score;
		public ImageView image;
	}
}
